/**
 * 
 */
package com.kylin.frame.core.activerecord;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.TableInfo;
import com.jfinal.plugin.activerecord.TableInfoMapping;
import com.kylin.erp.dbmodel.TMembers;
import com.kylin.frame.web.SessionMember;
import com.kylin.frame.web.model.DataTablesModel;

/**
 * @author huxiang
 * 
 */
@SuppressWarnings({ "rawtypes" })
public abstract class DbModel<T extends DbModel> extends Model<T> {

	/** * 保存变量的ThreadLocal，保持在同一线程中同步数据. */
	private static ThreadLocal<SessionMember> sessionob = new ThreadLocal<SessionMember>() {

		public SessionMember initialValue() {

			return null;
		}
	};

	private static final long serialVersionUID = -6215428115177000482L;

	private static final TableInfoMapping tableInfoMapping = TableInfoMapping
			.me();

	public static void setSessionOb(SessionMember sm) {
		sessionob.set(sm);
	}

	/**
	 * 用来针对DataTables封装的分页查询
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @param select
	 * @param sqlExceptSelect
	 * @param paras
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public DataTablesModel paginateDataTables(int pageNumber, int pageSize,
			String select, String sqlExceptSelect, Object... paras) {

		Page<T> pages = super.paginate(pageNumber, pageSize, select,
				sqlExceptSelect, paras);

		TableInfo tInfo = tableInfoMapping.getTableInfo(getClass());
		List<String> ids = new ArrayList<String>();
		List<List<String>> cells = new ArrayList<List<String>>();
		for (int i = 0; i < pages.getList().size(); i++) {
			T t = pages.getList().get(i);

			Map attrs = t.getAttrs();
			Set<String> key = attrs.keySet();
			List<String> cell = new ArrayList<String>();
			for (Iterator it = key.iterator(); it.hasNext();) {
				String s = (String) it.next();
				if (s.toLowerCase().equals(tInfo.getPrimaryKey().toLowerCase())) {
					ids.add(attrs.get(s).toString());
				} else {
					if (null != attrs.get(s)) {
						cell.add(attrs.get(s).toString());
					} else {
						cell.add("");
					}
				}
			}
			cells.add(cell);

		}

		return new DataTablesModel(pageNumber, pageSize, pages.getTotalRow(),
				ids, cells);
	}

	/**
	 * 用来针对DataTables封装的分页查询
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @param select
	 * @param sqlExceptSelect
	 * @return
	 */
	public DataTablesModel paginateDataTables(int pageNumber, int pageSize,
			String select, String sqlExceptSelect) {
		return this.paginateDataTables(pageNumber, pageSize, select,
				sqlExceptSelect, new Object[0]);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jfinal.plugin.activerecord.Model#save()
	 */
	@Override
	public boolean save() {

		if (null != DbModel.sessionob.get()) {
			Map<String, Object> attrs = new HashMap<String, Object>();
			TMembers member = DbModel.sessionob.get().getMember();
			attrs.put("create_user_", member.getInt("id_"));
			attrs.put("update_user_", member.getInt("id_"));
			attrs.put("create_date_", new Date());
			attrs.put("update_date_", new Date());
			this.setAttrs(attrs);
		}
		return super.save();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jfinal.plugin.activerecord.Model#update()
	 */
	@Override
	public boolean update() {

		if (null != DbModel.sessionob.get()) {
			Map<String, Object> attrs = new HashMap<String, Object>();
			TMembers member = DbModel.sessionob.get().getMember();
			attrs.put("update_user_", member.getInt("id_"));
			attrs.put("update_date_", new Date());
			this.setAttrs(attrs);
		}
		return super.update();
	}

}
